package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.Elefee;
import com.qf.entity.House;
import com.qf.entity.Owner;
import com.qf.entity.Property;
import com.qf.enums.AppHttpCodeEnum;
import com.qf.mapper.HouseMapper;
import com.qf.mapper.OwnerMapper;
import com.qf.mapper.PropertyMapper;
import com.qf.result.ResponseResult;
import com.qf.service.OwnerService;
import com.qf.service.PropertyService;
import com.qf.utils.BeanCopyUtils;
import com.qf.vo.ElefeeVo;
import com.qf.vo.OwnerVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Property)表服务实现类
 *
 * @author makejava
 * @since 2023-02-04 22:32:09
 */
@Service("propertyService")
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements PropertyService {

    @Resource
    PropertyMapper propertyMapper;

    @Resource
    OwnerMapper ownerMapper;

    @Resource
    HouseMapper houseMapper;

    @Override
    public ResponseResult findAll(String ownerName) {
        List<Property> properties = new ArrayList<>();
        if (ownerName==null||"".equals(ownerName)) {
            properties = propertyMapper.selectList(null);
            for (Property p : properties){
                Integer ownId = p.getOwnId();
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("id", ownId);
                Owner owner = ownerMapper.selectOne(queryWrapper1);
                p.setOwnerName(owner.getOwnName());
                p.setTotalPropertyFee(owner.getTotalPropertyFee());
                Integer houseId = p.getHouseId();
                QueryWrapper queryWrapper2 = new QueryWrapper();
                queryWrapper2.eq("id", houseId);
                House house = houseMapper.selectOne(queryWrapper2);
                p.setHouseNum(house.getHouseNum());
            }
        }else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.like("own_name", ownerName);
            List<Owner> list = ownerMapper.selectList(queryWrapper);
            for (Owner owner : list) {
                System.out.println(owner+"============");
                Integer oid = owner.getId();
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("own_id", oid);
                Property property1 = propertyMapper.selectOne(queryWrapper1);
                if (property1==null){
                    continue;
                }
                property1.setOwnerName(owner.getOwnName());
                Integer houseId = property1.getHouseId();
                queryWrapper1.eq("id",houseId);
                House house = houseMapper.selectOne(queryWrapper1);
                property1.setHouseNum(house.getHouseNum());
                properties.add(property1);
            }
        }
        return ResponseResult.okResult(properties);
    }

    @Override
    public ResponseResult delete(int id) {
        int i = propertyMapper.deleteById(id);
        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult add(Property property) {

        int i = propertyMapper.insert(property);

        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult selectOneByHouseId(Integer id) {
        OwnerVo ownerVo = new OwnerVo();
        QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
        QueryWrapper<House> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq("house_id",id);
        Owner owner = ownerMapper.selectOne(queryWrapper);
        queryWrapper1.eq("id", owner.getHouseId());
        House house = houseMapper.selectOne(queryWrapper1);
        ownerVo.setHouseNum(house.getHouseNum());
        ownerVo.setHouseId(owner.getHouseId());
        ownerVo.setId(owner.getId());
        ownerVo.setOwnName(owner.getOwnName());
        ownerVo.setCreateTime(owner.getCreateTime());
        ownerVo.setUpdateTime(owner.getUpdateTime());
        ownerVo.setOwnPhone(owner.getOwnPhone());
        ownerVo.setCarNum(owner.getCarNum());
        ownerVo.setParkId(owner.getParkId());
        ownerVo.setTotalPropertyFee(owner.getTotalPropertyFee());
        if (ownerVo != null){
            return ResponseResult.okResult(ownerVo);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

}
